import {PATHS} from '../const';
import {homeApi} from '../services';

const home = {
    namespace: 'home',
    state:{
        title: '首页',
        home_list: []
    },
    // 异步操作
    effects: {
        // 添加effects函数
        // call与put是dva的函数
        // call调用执行一个函数
        // put则是dispatch执行一个action
        // select用于访问其他model
        // https://github.com/dvajs/dva/blob/master/docs/API_zh-CN.md
        *query({payload},{ select, call, put}){
            const params = {
                api_version: '8.6.0',
                app_client_id:3,
                latitude:31.201341,
                longitude:121.633158,
                station_id: '53eb382d7f8b9ac3b18b4573'
            };
            const {data, error} = yield call(homeApi.getHomeDetail, params);
            if(data){
                console.log('成功数据...',data)
                const {list:home_list} = data;
                yield put({ type:'updateData',payload:{
                    home_list,
                }})
            }
            if(error){
                console.log('失败数据...',error)
            }
        }
    },
    // 替换状态树
    reducers: {
        updateData(state, {payload}){
            console.log('更新数据...',payload)
            return {...state, ...payload};
        }
    },
    subscriptions: {
        setup({dispatch, history}) {
            history.listen(location => {
                if(location.pathname !== PATHS.PAGE_HOME){
                    return;
                }
                dispatch({
                    type: 'query',
                    payload: {}
                });
                console.log('history...',history.location,PATHS.PAGE_HOME)
            });
        },
    }
};
export default home;